import PageModel from "../../models/Page";
import ImageModel from "../../models/Image";

import CartModel from "../../models/Cart";
import Crypt from "../../utils/Crypt";
import CartTotal from "../../utils/CartTotal";

const cartModel = new CartModel()

const pageModel = new PageModel()
const imageModel = new ImageModel()

Page({

  // onTabItemTap(item) {
  //   // console.log(item)
  //   pageModel.getWx().then(response => {
  //     const { data } = response
  //     wx.setStorageSync('wx-set', data.wx)
  //   })
  // },

  data: {
    addressList: [],
    wx: false,
    city: ''
  },

  onShow() {
    pageModel.getWx().then(response => {
      const { data } = response
      wx.setStorageSync('wx-set', data.wx)
      this.setData({ wx: data.wx })
    })

    // 抽奖数量
    pageModel.getAwardCount().then(response => {
      const { data } = response
      this.setData({
        award: data.count
      })
    })
  },

  // 查看奖列表
  onAward() {
    wx.navigateTo({
      url: '/sub_main/pages/award/index'
    })
  },

  // 定位
  getLocation() {
    wx.getLocation({
      type: 'wgs84',
      success: (res) => {
        const latitude = res.latitude
        const longitude = res.longitude
        console.log(latitude, longitude)
        // 使用经纬度获取地理位置信息
        wx.request({
          url: 'https://api.map.baidu.com/geocoder/v2/?ak=' +  getApp().globalData.baiduMapAK + '&location=' + latitude + ',' + longitude + '&output=json',
          data: {},
          header: {
            'Content-Type': 'application/json'
          },
          success: (res) => {
            if (res.data.status === 0) {
              const city = res.data.result.addressComponent.city
              wx.setStorageSync('wx_location_city', city)
              console.log(city, res.data)
              this.setData({
                city: city
              });
            }
          }
        })
      },
      fail: (err) => {
        console.log(err)
      }
    })
  },

  onLoad: function (options) {

    // 定位
    // this.getLocation()

    // console.log(options)

    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline'],
      success: () => {
        // console.log('ok')
        // 可以把分享记录保存到数据库
      }
    })

    // uni
    pageModel.getHome().then(response => {
      let { data } = response
      data = Crypt.parse(data)
      this.setData({ ...data })
      console.log(this.data)
    }).catch(err => {

    })

    // 地区
    pageModel.getAddress().then(response => {
      let { data } = response
      data = Crypt.parse(data)
      this.setData({ addressList: data.list })
      // console.log(this.data.addressList)
    })

    // image
    imageModel.getRemoteImage().then(data => {
      this.setData({
        image: data
      })
      // console.log(this.data.image)
    }).catch(err => {

    })

    // referrer
    if (options.scene && (options.scene > 0 || options.scene.length > 0)) {

      console.log(options.scene)

      let id = 0
      let product_id = 0
      if (options.scene.indexOf('-') > -1) {
        const temp = options.scene.split('-')
        if (temp && temp.length === 2) {
          id = temp[1]
          product_id = temp[0]
        }
      } else {
        id = options.scene
      }

      if (product_id != 'merchant') {
        pageModel.setReferrer(id)
      }

      // console.log('推荐人ID: ', id)

      // // 如果是线下支付
      if (product_id == 'merchant') {
        console.log(id, 222)
        wx.navigateTo({
          url: '/sub_main/pages/merchant-pay-run/index?id=' + id
        })
      }

      if (product_id > 0) {

        wx.navigateTo({
          url: '/sub_product/pages/detail/index?id=' + product_id
        })

      }
    }

    // 购物车数量 (这个不用登录)
    cartModel.listTotal().then(response => {
      let { data } = response
      data = Crypt.parse(data)
      CartTotal.total(data)
    })


  }
});
